package com.gc.baselib.base;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gc.baselib.core.CoreApi;
import com.gc.baselib.img.ImageLoader;

import com.gc.baselib.manager.FilePathManager;
import com.gc.baselib.storage.MMKVStorage;
import com.gc.baselib.utils.SysUtils;
import com.gc.libnet.HttpManager;
import com.gc.logger.LogUtils;

/**
 * @author admin
 * @Date 2022/6/9 17:52
 * @package： com.zk.baselib.base
 * @decrpition
 */
public class BaseApplication extends Application implements Application.ActivityLifecycleCallbacks {
    private static Application sInstance;
    /**
     * 获得当前app运行的Application
     */
    public static Application getInstance() {
        return sInstance;
    }
    private int mActivityCount = 0;
    @Override
    public void onCreate() {
        super.onCreate();
//        ProcessLifecycleOwner.get().getLifecycle().addObserver(new AppLifeCycle());
        if (SysUtils.isMainProcess(this)) {
            initApplication(this);
            initThirdLibrary();
            initNavPath();
        }
    }
    protected void initNavPath(){

    }
    protected void initThirdLibrary() {
//        ARouter.init(this);
//        ARouter.openDebug();
//        ARouter.openLog();
    }

    /**
     * 初始化Application
     * @param application
     */
    protected void initApplication(Application application) {
        sInstance=application;
        registerActivityLifecycleCallbacks(this);
        CoreApi.getInstance().init(this);
        FilePathManager.INSTANCE.initContext(this);
        MMKVStorage.getInstance().init(sInstance);
        initLogger(this);
        initNet(this);
        ImageLoader.init(this);
        //DialogX初始化
    }

    protected void initNet(Application application) {
        HttpManager.getINSTANCE().init(application, (tag, message, throwable, isDebug) -> {
            if(isDebug){
                LogUtils.d(tag,message,throwable);
            }else{
                LogUtils.e(tag,message,throwable);
            }
            return null;
        });
    }

    protected void initLogger(Application application) {
        LogUtils.getINSTANCE().init(application);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
        ImageLoader.trimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        ImageLoader.clearMemory();
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
        LogUtils.d("framework",activity.getClass().getSimpleName()+":onCreate");
        ActivityManager.getInstance().add(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        mActivityCount++;
        if(mActivityCount==1){
            LogUtils.d("framework","应用前台运行");
        }
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        ActivityManager.getInstance().onResumed(activity);
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        ActivityManager.getInstance().onPause(activity);
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        mActivityCount--;
        if(mActivityCount==0){
            LogUtils.d("framework","应用后台运行");
        }
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        LogUtils.d("framework",activity.getClass().getSimpleName()+":Destroy");
        ActivityManager.getInstance().remove(activity);
    }
}
